package com.tenglong.user.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.tenglong.iservice.pojo.Service;
import com.tenglong.iservice.service.IserviceService;
import com.tenglong.user.pojo.User;
import com.tenglong.user.service.UserService;
import com.tenglong.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Chen-D.W on 2018/5/23.
 */
@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private IserviceService iserviceService;

    private static final int RESULT_STATUS=200;
    /**
     * 用户列表展示
     */
    @RequestMapping("/right")
    public String  userList(HttpServletRequest request, HttpServletResponse response, Model model,@RequestParam(value = "pageNum",defaultValue = "1") int page) throws ServletException, IOException {

        String cartNo=request.getParameter("cartNo");
        int pageNum= page;
        int pageSize = Constant.DEFAULT_PAGE_SIZE;
        //调用service 获取查询结果
        Pager<User> result = userService.findStudent(cartNo,
                pageNum, pageSize);

        // 返回结果到页面
        model.addAttribute("result", result);
        if(cartNo==null){
            model.addAttribute("cartNo","");
        }else {
            model.addAttribute("cartNo", cartNo);
        }
        return "right";
    }
    /**
     * @Author 陈董伟  edu.zzuli
     * Date 2018/5/23 14:51
     * @Descrintion 添加客户
     */
    @RequestMapping("/addUser")
    public String adduser(@RequestParam(value = "id",defaultValue = "-1")String  id,Model model){
        BaseResult result=userService.getUserDetailsById(id);
        if (result.getStatus()==RESULT_STATUS){
            model.addAttribute("user",result.getData());
            BaseResult baseResult=iserviceService.getUserServiceById(id,1L);
            model.addAttribute("service",baseResult);
            model.addAttribute("count",Count.getCount(iserviceService.getService(id)));
        }else {
            User user=new User();
            user.setId("");
            user.setUserCreated("");
            user.setUserBrand("");
            user.setUserCarNo("");
            user.setUserKm("");
            user.setUserPhone("");
            user.setUserModuls("");
            user.setUserTime("");
            user.setUserName("");
            model.addAttribute("count",new Count());
            model.addAttribute("user",user);
        }
        return "tab";
    }
    @RequestMapping("/adduser")
    @ResponseBody
    public BaseResult addu(HttpServletRequest request,User user){
        return userService.addUser(user);
    }
    @RequestMapping("/delUser")
    @ResponseBody
    public BaseResult deluser(@RequestParam("id")String  id){
        BaseResult result=userService.removeUserById(id);
        return result;
    }
    /**
     * @Author chendongwei
     * Date 2018-05-31 18:29
     * @Descrintion 导出客户信息到excel
     */
    @RequestMapping("/out")
    @ResponseBody
    public BaseResult outExcel(){
        BaseResult result=userService.outExcel();
        return result;
    }

}
